package com.baturu.vin.hyundai.service.impl;

import com.baturu.vin.enums.LangEnum;
import com.baturu.vin.hyundai.dal.dao.HyundaiKiaLJDDAO;
import com.baturu.vin.hyundai.dal.entity.SEPCHyundaiSubGroupEntity;
import com.baturu.vin.hyundai.dto.HyundaiKiaPartsMappingDTO;
import com.baturu.vin.hyundai.dto.HyundaiKiaVehicleDTO;
import com.baturu.vin.hyundai.dto.KiaPartsDTO;
import com.baturu.vin.kit.StreamKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: chenjiahao
 * @Time: 2018/12/5
 */
@Service
public class LJDVinSerivce {

    @Autowired
    private HyundaiKiaLJDDAO hyundaiKiaLJDDAO;

    protected final int SEGMENT_QUERY_SIZE = 150;

    public List<KiaPartsDTO> queryLJDParts(Set<String> partsCodes, HyundaiKiaVehicleDTO analyseResult) {

        return StreamKit.partitionStream(partsCodes, SEGMENT_QUERY_SIZE)
                .flatMap(l -> hyundaiKiaLJDDAO.queryLJDPartsByMapping(analyseResult.getCarTypeYear(), analyseResult.getAreaNum(), analyseResult.getMicd(), analyseResult.getOcn(), analyseResult.getVer(), analyseResult.getMakeDate(), l).stream())
                .distinct()
                .collect(Collectors.toList());
    }

    public List<KiaPartsDTO> queryLJDPartsByPreFix(String partsCodes, HyundaiKiaVehicleDTO analyseResult, String lang) {

        return LangEnum.ZH.getLang().equals(lang)
                ? hyundaiKiaLJDDAO.queryLJDPartsByPrePartsCode(analyseResult.getCarTypeYear(), analyseResult.getAreaNum(), analyseResult.getMicd(), analyseResult.getOcn(), analyseResult.getVer(), analyseResult.getMakeDate(), partsCodes)
                : hyundaiKiaLJDDAO.queryLJDPartsByPrePartsCodeEn(analyseResult.getCarTypeYear(), analyseResult.getAreaNum(), analyseResult.getMicd(), analyseResult.getOcn(), analyseResult.getVer(), analyseResult.getMakeDate(), partsCodes);
    }

    public List<KiaPartsDTO> queryLJDPartsByGroup(SEPCHyundaiSubGroupEntity sepcHyundaiSubGroupEntity, HyundaiKiaVehicleDTO analyseResult, String lang) {
        if (Objects.isNull(sepcHyundaiSubGroupEntity)) {
            return Collections.emptyList();
        }
        if (LangEnum.ZH.getLang().equals(lang)) {
            return hyundaiKiaLJDDAO.queryLJDPartsByGroup(sepcHyundaiSubGroupEntity, analyseResult);
        } else {
            return hyundaiKiaLJDDAO.queryLJDPartsByGroupEn(sepcHyundaiSubGroupEntity, analyseResult);
        }
    }

    public List<KiaPartsDTO> queryLJDPartsByPartsMappingV2(List<HyundaiKiaPartsMappingDTO> partsMappings, HyundaiKiaVehicleDTO analyseResult) {
        return StreamKit.partitionStream(partsMappings, SEGMENT_QUERY_SIZE)
                .flatMap(l -> hyundaiKiaLJDDAO.queryLJDPartsByMappingV2(analyseResult, l).stream())
                .distinct()
                .collect(Collectors.toList());
    }
}
